package com.example.file_explorer.controller;

import com.example.file_explorer.dto.LoginDto;
import com.example.file_explorer.result.Code;
import com.example.file_explorer.result.Result;
import com.example.file_explorer.service.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public Login login;

    @PostMapping
    public Result<Object> login(@RequestBody LoginDto loginDto) {
        if (login.login(loginDto.getUsername(), loginDto.getPassword())) {
            return Result.success("登录成功");
        }
        return Result.success("登录失败");
    }

    @GetMapping("/logout")
    public Result<Object> logout() {
        login.logout();
        return Result.success();
    }

    @GetMapping("/check")
    public Result<Object> checkLogin() {
        if (login.loginCheck()) {
            return Result.success();
        }else {
            return Result.error(Code.NOT_LOGIN.getCode(), "未登录");
        }
    }
}
